package epg;

import java.io.*;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Properties;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;

import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;

public class Main {
	

	public static void main(String[] args) throws Exception {
		Epigraph epg = null;		
		if(args.length == 0)
		epg = new Epigraph("TEST/testcode.txt", true);
		else
			epg = new Epigraph(args[0], true);

		if (epg.isDebug) {
			StringBuilder log = epg.getLog();
			System.out.print(log);
		}
		/*
		 * File program = new File("TEST/testcode.txt"); ANTLRNoCaseFileStream
		 * input = new ANTLRNoCaseFileStream( program.getAbsolutePath());
		 * epigraphLexer lexer = new epigraphLexer(input); // CommonTokenStream
		 * tokens = new CommonTokenStream(lexer); TokenStream tokens = new
		 * CommonTokenStream(lexer); epigraphParser parser = new
		 * epigraphParser(tokens); CommonTree tree = (CommonTree)
		 * parser.prog().getTree();
		 * 
		 * visit(tree, "");
		 * 
		 * CodeGenerator codeGen = null;
		 * 
		 * if (parser.getNumberOfSyntaxErrors() == 0) { codeGen = new
		 * CodeGenerator(tokens); codeGen.handle(tree);
		 * System.out.println(codeGen.getSource()); } //codeGen = null; if
		 * (codeGen != null && !codeGen.error) { String out = "TEST"; String
		 * file = out + "/Main.java"; save(file, codeGen.getSource());
		 * compile(file); // run("TEST.Main"); }
		 */
	}

	static void visit(Tree tree, String s) {
		// System.out.println(s + "text: " + tree.getText() + ", type: " +
		// tree.getType() + ", childs: " + tree.getChildCount());
		System.out.println(s + "text: " + tree.getText() + ", childs: "
				+ tree.getChildCount());
		for (int i = 0; i < tree.getChildCount(); i++) {
			visit(tree.getChild(i), s + "  ");
		}
	}

	static String getBase() {
		return System.getProperty("user.dir").replace('\\', '/');
	}

	static void save(String file, StringBuilder code) throws IOException {
		OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(
				file));
		out.append(code);
		out.close();
	}

	static void compile(String file) throws IOException {
		System.out.println("compile: " + file);
		// JavaCompiler compiler = javax.tools.ToolProvider
		// .getSystemJavaCompiler();
		JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
		try {
			compiler.run(System.in, System.out, System.err, file);
		} catch (Exception e) {
			System.out.println(e);
			System.exit(0);
		}
	}

	static void run(String file) throws Exception {
		System.out.println();
		String base = "file://" + getBase() + "/";
		System.out.println("Base: " + base);
		URL url = new URL(base);
		URLClassLoader classLoader = new URLClassLoader(new URL[] { url });
		// Class main = classLoader.loadClass("simple.Main");
		Class main = classLoader.loadClass(file);

		Object obj = main.newInstance();
		String[] args = new String[] { "1", "2" };
		Object[] passedArgs = new Object[] { args };
		Method mainMethod = main.getMethod("main", args.getClass());
		mainMethod.invoke(obj, passedArgs);
	}
}